package com.xiliu.rocketmq.order;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @author xiliu
 * @description 顺序消息生产者
 * @date 2022/3/19 20:12
 */
public class OrderProducer {

    public static void main(String[] args) throws Exception{
        DefaultMQProducer producer = new DefaultMQProducer("xiliu_producer_group");
        producer.setNamesrvAddr("42.194.222.32:9876");
        producer.start();

        for (int i = 0; i<100; i++) {
            String msgStr = "order -->" + i;
            // 模拟生产订单id
            int orderId = i % 10;

            Message message = new Message("broker-a","order_msg",msgStr.getBytes(RemotingHelper.DEFAULT_CHARSET));

            SendResult sendResult = producer.send(message,(mqs, msg, arg) -> {
                    Integer id = (Integer) arg;
                    int index = id % mqs.size();
                    return mqs.get(index);
                    }, orderId);
            System.out.print(sendResult);
        }
        producer.shutdown();
    }
}
